<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/12 16:52}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\controller;
use app\common\controller\Api;
use app\api\model\User as UserModel;
use app\api\model\Cart as CartModel;
use app\api\model\Config as ConfigModel;
use think\Request;
use app\api\model\UserAddress as UserAddressModel;
use app\api\model\GoodsItem;

class Cart extends Api
{
    protected $noNeedLogin = [
        'Cartdel',
        'change',
        'Cartaddress',
        'Addcart',
    ];
    protected $noNeedRight = '*';


    /**
     *
     * 添加商品购物车
     */
    public function Addcart()
    {
        $uid       = $this->auth->id;
        $sku_id    = $this->request->param('sku_id', '');   // 规格 id
        $gid       = $this->request->param('id');          //商品id
        $goods_num = $this->request->param('goods_num',1); //数量

        if (empty($gid)) $this->success('请选择您的商品', '', 401);

        $UserModel = new UserModel();
        $user = $UserModel->getUser($uid);
        if (!$user) $this->success('用户信息不存在请从新登录', '', 401);

        $CartModel = new CartModel();
        $response = $CartModel->add($gid, $sku_id, $uid, $goods_num);
        if ($response['status']) {
            $this->success($response['msg'], $response['data'], 200);
        } else {
            $this->success($response['msg'], '', 400);
        }
    }
    /**
     * 购物车列表
     */
    public function Cartlist()
    {
        $uid = $this->auth->id;

        $CartModel = new CartModel();
        $data = $CartModel->cartlist($uid);
        if ($data)
        {
            $this->success('查询成功',$data,200);
        }else{
            $this->success('查询成功',[],200);
        }
    }
    /**
     * 购物车添加数量
     */
    public function change()
    {
        $id = $this->request->param('id');
        $cartnum = $this->request->param('num');
        if (!$id) $this->success('缺失参数','',400);

        $CartModel = new CartModel();
        $cartres = $CartModel->change($id, $cartnum);

        if ($cartres['status'])
        {
            $this->success($cartres['msg'],'',200);
        }else{
            $this->success($cartres['msg'],'',400);
        }
    }
    /**
     * 删除
     */
    public function Cartdel()
    {
        $uid = $this->auth->id;
        $ids = $this->request->param('ids');
        $CartModel = new CartModel();
        $res = $CartModel->del($ids,$uid);
        if ($res)
        {
            $this->success('删除成功',$res,200);
        }else{
            $this->success('删除失败','',400);

        }
    }
    /**
     * 结算页
     * @param $type 地址类型:0-默认地址,1-安装店地址
     * @param $ids 商品id,多商品已逗号分开
     * @param $addressId 地址id
     * @param $goodstype 0 = 立即购买, 1 = 购物车购买
     * @param $goods_num 商品数量
     */
    public function Checkout()
    {
        $uid  = $this->auth->id;
        $ids  = $this->request->param('ids');
        // dump($ids);
        // die;
        $skuid  = $this->request->param('sku_id');
        $type   = $this->request->param('type',0);
        $goods_num   = $this->request->param('goods_num',0);
        $collocation = $this->request->param('collocation',0);
        // dump($collocation);
        // die;
        $share = $this->request->param('share',0);

        $CartModel = new CartModel();
        // 结算页
        $cartlist  = $CartModel->Getsettlement($uid,$ids,$type,$goods_num,$share,$collocation,$skuid);
        // dump($cartlist);
        // die;
        $UserAddressModel = new UserAddressModel();
        // 获取用户地址
        $address = $UserAddressModel->Getuseraddress($uid);
        //获取偏远地区
        $ConfigModel = new ConfigModel();
        $postage = $ConfigModel->getRemoteAreas();
        $interPrice = $ConfigModel->interPrice();
        $cartlist['data']["interPrice"] = $interPrice;
        // dump($cartlist['data']);
        // die;
        $cartlist['data']["pay_price_new"] = $cartlist['data']["pay_price"];
        if(in_array($address["province"],$postage)){
            $postage_price = $ConfigModel->getRemoteAreasPrice();
            $cartlist['data']["postage"] = $postage_price;
            $cartlist['data']["pay_price"] = $cartlist['data']["pay_price"] + $postage_price;
        };
        if ($cartlist['status'])
        {
            $data = [
                'goods' => $cartlist['data'],
                'address' => $address
            ];
            $this->success($cartlist['msg'], $data,200);
        }else{
            $this->success($cartlist['msg'],'',400);
        }

    }

    //修改地址回调
    public function CheckoutCallback(){
        $uid  = $this->auth->id;
        $addressId  = $this->request->param('addressId');
        $pay_price_new = $this->request->param('pay_price_new');
        $UserAddressModel = new UserAddressModel();
        // 获取用户地址
        $address = $UserAddressModel->getAddress($addressId);
        //获取偏远地区
        $ConfigModel = new ConfigModel();
        $postage = $ConfigModel->getRemoteAreas();
        // $interPrice = $ConfigModel->interPrice();
        // $cartlist['data']["interPrice"] = $interPrice;
        // dump($cartlist['data']);
        // die;
        // $cartlist['data']["pay_price_new"] = $cartlist['data']["pay_price"];
        if(in_array($address["province"],$postage)){
            $postage_price = $ConfigModel->getRemoteAreasPrice();
            // $cartlist['data']["postage"] = $postage_price;
            $new_price = $pay_price_new + $postage_price;
        }else{
            $new_price = $pay_price_new;
        }
        $this->success("获取成功", $new_price,200);
        
    }

    // 修改-----备份
//    public function Checkout()
//    {
//        $uid  = $this->auth->id;
//        $ids  = $this->request->param('ids');
//        $skuid  = $this->request->param('sku_id');
//        $type   = $this->request->param('type',0);
//        $goods_num   = $this->request->param('goods_num',0);
//        $collocation = $this->request->param('collocation',0);
//        $share = $this->request->param('share',0);
//
//        $CartModel = new CartModel();
//        $cartlist  = $CartModel->Getsettlement($uid,$ids,$type,$goods_num,$share,$collocation,$skuid);
//
//
//        $UserAddressModel = new UserAddressModel();
//        $address = $UserAddressModel->Getuseraddress($uid);
//
//        if ($cartlist['status'])
//        {
//            $data = [
//                'goods' => $cartlist['data'],
//                'address' => $address
//            ];
//            $this->success($cartlist['msg'], $data,200);
//        }else{
//            $this->success($cartlist['msg'],'',400);
//        }
//
//    }
    // 修改-----备份
    /**
     * 收货地址
     */
    public function Cartaddress()
    {
        $id = $this->request->param('id');
        $type = $this->request->param('type',1);

        $UserAddressModel = new UserAddressModel();
        if ($type == 1)
        {
            $address = $UserAddressModel->getAddress($id);
        }else{
            $address = $UserAddressModel->getshopaddress($id);
        }
        if (!$address) $this->success('地址信息不存在', '', 400);

        $this->success('获取成功', $address, 200);
    }

    /**
     * 修改购物车
     */
    public function update()
    {
        $uid = $this->auth->id;
        $id  = $this->request->param('id');
        $gid = $this->request->param('gid');
        $sku_id = $this->request->param('sku_id');

        if (empty($id) || empty($gid) || empty($sku_id))
        {
            $this->success('缺少参数请从新操作','',400);
        }

        $cart = CartModel::get($id);

        if (empty($cart) || $cart['uid'] != $uid || $cart['gid'] != $gid)
        {
            $this->success('该数据不存在','',400);
        }

        $goodsItem = GoodsItem::where('goods_id',$gid)->where('id',$sku_id)->find();
        if (empty($goodsItem))
        {
            $this->success('暂无商品', '', 400);
        }
        if ($goodsItem['stock'] <= 0)
        {
            $this->success('库存不足', '', 400);
        }

        $cart->gid         = $goodsItem['goods_id'];
        $cart->sku_id      = $goodsItem['id'];
        $cart->goods_num   = 1;
        $cart->update_time = time();
        $cart->save();

        if ($cart->id)
        {
            $this->success('修改成功', '', 200);
        }else{
            $this->success('操作失败', '', 400);
        }


    }
}